package com.example.practice;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import static android.text.TextUtils.isEmpty;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    SharedPreferences sp;
    private Button mSetting;
    private EditText mUser;
    private EditText mPassword;
    private CheckBox mSPassword;
    private CheckBox mSReg;
    private Button mReg;
    private Button mLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        Logo();
        initView();
        sp=getSharedPreferences("m_true",MainActivity.MODE_PRIVATE);
        String msr = sp.getString("msr", "");
        String msp = sp.getString("msp", "");
        mSReg.setChecked(Boolean.parseBoolean(msr));
        mSPassword.setChecked(Boolean.parseBoolean(msp));
        if( Boolean.parseBoolean(msp)){
            sp=getSharedPreferences("login",MainActivity.MODE_PRIVATE);
            mUser.setText(sp.getString("m_user",""));
            mPassword.setText(sp.getString("m_password",""));
        }
        if(Boolean.parseBoolean(msr)){
            if(submit()){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        checkBox();
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        startActivity(new Intent(MainActivity.this,SuccessActivity.class));}
                }).start();
            }
            else {
                Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void Logo() {
        sp = getSharedPreferences("sign", MainActivity.MODE_PRIVATE);
        String sign = sp.getString("sign", "");
        if (isEmpty(sign)) {
            startActivity(new Intent(MainActivity.this, LogoActivity.class));
        }
    }

    private void initView() {
        mSetting = (Button) findViewById(R.id.m_setting);
        mSetting.setOnClickListener(this);
        mUser = (EditText) findViewById(R.id.m_user);
        mUser.setOnClickListener(this);
        mPassword = (EditText) findViewById(R.id.m_password);
        mPassword.setOnClickListener(this);
        mSPassword = (CheckBox) findViewById(R.id.m_s_password);
        mSPassword.setOnClickListener(this);
        mSReg = (CheckBox) findViewById(R.id.m_s_reg);
        mSReg.setOnClickListener(this);
        mReg = (Button) findViewById(R.id.m_reg);
        mReg.setOnClickListener(this);
        mLogin = (Button) findViewById(R.id.m_login);
        mLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.m_setting:
                startActivity(new Intent(MainActivity.this,SettingActivity.class));
                break;
            case R.id.m_reg:
                if(submit()){
                    checkBox();
                startActivity(new Intent(MainActivity.this,SuccessActivity.class));}
                else {

                }
                break;
            case R.id.m_login:
                startActivity(new Intent(MainActivity.this,LoginActivity.class) );
                break;
            case R.id.m_s_reg :
                if(mSReg.isChecked()){mSPassword.setChecked(true);}
                break;
            case R.id.m_s_password :
                if(!mSPassword.isChecked()){mSReg.setChecked(false);}
                break;
        }
    }

    private boolean submit() {
        Boolean b =false;
        // validate
        String user = mUser.getText().toString().trim();
        String password = mPassword.getText().toString().trim();
        if(mSPassword.isChecked()){
            sp=getSharedPreferences("login",MainActivity.MODE_PRIVATE);
            SharedPreferences.Editor edit = sp.edit();
            edit.putString("m_user",user);
            edit.putString("m_password",password);
            edit.apply();
        }
        SQLiteDatabase db = openOrCreateDatabase("info", MODE_PRIVATE, null);
        Cursor info1 = db.query("info", null, null,
                null, null, null, null);
        if(info1!= null){
            while(info1.moveToNext()) {
                if(info1.getString(info1.getColumnIndex("info_user")).equals(user)
                        &&info1.getString(info1.getColumnIndex("info_password")).equals(password)){
                    b = true;
                    break;
                }else {
                    Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        }else {
            Toast.makeText(MainActivity.this,"服务器异常",Toast.LENGTH_SHORT).show();
        }
        return  b ;
    }
    private void checkBox(){
        sp= getSharedPreferences("m_true", MainActivity.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("msp", String.valueOf(mSPassword.isChecked()));
        edit.putString("msr", String.valueOf(mSReg.isChecked()));
        edit.apply();
    }
}
